package com.ruoyi.expand.service.impl;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.expand.service.CountryInfoService;
import com.ruoyi.expand.entity.CountryInfo;

import org.hibernate.validator.cfg.defs.br.CNPJDef;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.plugin.spring.boot.service.BaseService;

import org.nutz.plugin.spring.boot.service.entity.Pager;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @author zhangjialu
 * @date 2021/7/31 下午11:23
 */
@Service
public class CountryInfoServiceImpl extends BaseService<CountryInfo> implements CountryInfoService {

    @Override
    public TableDataInfo listPage(CountryInfo countryInfo, Integer pageNum, Integer pageSize) {

        Sql sql = Sqls.queryEntity("select * from bic_country t1 where  del_flag ='0' and (country_code like @countryCode or cn_name like @cnName)");
        sql.params().set("countryCode","%"+countryInfo.getCountryCode()+"%").
                set("cnName",countryInfo.getCountryCode()+"%");


        sql.setPager(this.dao().createPager(pageNum,pageSize));

        sql.setEntity(this.dao().getEntity(CountryInfo.class));
        this.dao().execute(sql);
        List<CountryInfo> list = sql.getList(CountryInfo.class);


        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);

        int total = this.count(Cnd.NEW().and("countryCode","like","%"+countryInfo.getCountryCode()+"%").
                and("cnName","like",countryInfo.getCountryCode()+"%").
                and("del_flag","=","0"));

        rspData.setTotal(total);

        return rspData;
    }

    @Override
    public AjaxResult saveBPrivateRoom(CountryInfo countryInfo) {
        Date currentDate = new Date();
        if(StringUtils.isBlank(countryInfo.getId())){
            countryInfo.setCreateTime(currentDate);
            countryInfo.setUpdateTime(currentDate);
            countryInfo.setDelFlag("0");
            this.save(countryInfo);
            if(StringUtils.isNotEmpty(countryInfo.getId())){
                return AjaxResult.success();
            }
        }else{
            countryInfo.setUpdateTime(currentDate);
            int count = this.updateIgnoreNull(countryInfo);
            if(count>0){
                return AjaxResult.success();
            }
        }
        return AjaxResult.error();
    }

    @Override
    public Object selectJobById(String bPrivateRoomId) {

        return this.fetch(bPrivateRoomId);
    }

    @Override
    public void deleteBPrivateRoomByIds(String ids) {
        if(StringUtils.isNoneEmpty(ids)){
            String idList[] = ids.split(",");
            for(int i=0;i<idList.length;i++){
                this.dao().update(CountryInfo.class, Chain.make("delFlag","1"), Cnd.NEW().and("id","=",idList[i]));
            }
        }
    }


    @Override
    public List<CountryInfo> selectCountryList(CountryInfo countryInfo) {
        Sql sql = Sqls.queryEntity("select * from bic_country t1 where  del_flag ='0' and (country_code like @countryCode or cn_name like @cnName)");
        sql.params().set("countryCode","%"+countryInfo.getCountryCode()+"%").
                set("cnName",countryInfo.getCountryCode()+"%");

        sql.setEntity(this.dao().getEntity(CountryInfo.class));
        this.dao().execute(sql);
        List<CountryInfo> resultInfo = sql.getList(CountryInfo.class);

        return resultInfo;
    }

}
